<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="800px" append-to-body
    top="8vh">
    <el-container>
      <el-main style="max-height:70vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
          label-width="130px">
          <el-form-item label="奖金项分类" prop="jjxFl">
            <el-select :clearable="true" v-model="dataForm.jjxFl" placeholder="请选奖金项分类" class="xiaLa" style="width: 90%">
              <el-option v-for="(item,index) in  jjflOptions " :key="index" :label="item.label" :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="奖金项名称" prop="jjxMc">
            <el-input v-model="dataForm.jjxMc" placeholder="奖金项名称" style="width: 90%"></el-input>
          </el-form-item>
          <!-- <el-form-item label="应用状态" prop="jjxZt">
            <el-select :clearable="true" v-model="dataForm.jjxZt" placeholder="请选择状态"  style="width: 90%">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="奖金项说明" prop="jjxSm">
            <el-input type='textarea' v-model="dataForm.jjxSm" placeholder="奖金项说明" rows="5" style="width: 90%">
            </el-input>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getObj,
    addObj,
    putObj
  } from '@/api/jiangjin/jiangjinjjxxxb'
  import {
    remote as remoteDict
  } from '@/api/admin/dict'

  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          jjxFl: '',
          jjxMc: '',
          jjxSm: '',
          jjxZt:'',
          jjDeptId :0
        },
        options: [{
          value: 1,
          label: '显示'
        }, {
          value: 0,
          label: '隐藏'
        }],
        jjflOptions: [],
        dataRule: {
          jjxFl: [{
              required: true,
              message: '奖金项分类不能为空',
              trigger: 'blur'
            },

          ],
          jjxMc: [{
              required: true,
              message: '奖金项名称不能为空',
              trigger: 'blur'
            },
            {
              max: 100,
              message: '长度最长100个字符',
              trigger: 'blur'
            }
          ],
          jjxSm: [{
            max: 500,
            message: '长度最长500个字符',
            trigger: 'blur'
          }],
        }
      }
    },
    created() {
      remoteDict("jingjinFenLei").then(response => {
        this.jjflOptions = response.data.data
      })
    },
    methods: {
      init(id,deptId) {
        console.log(id)
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }else {
            this.dataForm.jjDeptId = deptId;
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            ////console.log(this.dataForm);
            // return;
            //===========

            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      }
    }
  }

</script>
